/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>Project: javase2203 - AtomicIntegerDemo
 * <p>Powered by webrx On 2022-08-09 09:56:20
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class AtomicIntegerDemo {
    //AtomicInteger count = new AtomicInteger(0);
    int count = 0;
    //void  m() {
    //    for(int i=0; i<100000; i++) count.incrementAndGet();
    //}

    synchronized void m() {
        for (int i = 0; i < 1000000; i++)            {
            count++;
        }
    }

    // void m() {
    //    for (int i = 0; i < 100000; i++) {
    //        synchronized (this) {
    //            count++;
    //        }
    //    }
    //}


    public static void main(String[] args) {
        long ss = System.currentTimeMillis();
        AtomicIntegerDemo t = new AtomicIntegerDemo();
        List<Thread> threads = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            threads.add(new Thread(t::m, "thread-" + i));
        }
        threads.forEach((o) -> o.start());
        threads.forEach((o) -> {
            try {
                o.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        System.out.println(t.count);

        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (Exception e) {
            e.printStackTrace();
        }
        long ee = System.currentTimeMillis();
        System.out.println(ee - ss);


    }
}
